﻿using System;
using System.Data;
using System.IO;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_User_Manage_Right : System.Web.UI.Page
{
    BaiMaSoft.SysManage.UserManage MYUM = new BaiMaSoft.SysManage.UserManage();

    UserManage MyUM = new UserManage();
    RoleManage MyRM = new RoleManage();
    BaimasoftFile MyFile = new BaimasoftFile();
    public string sSLogonID = "", sSRoleName = "", UserID = "", sCodeCol = "", sThisCode = "", UserRole = "";
    public int AllCount = 0, AllPageCount = 0, NowPageCount = 0, PageSize = 0;
    DataView MyDV;

    protected void Page_Load(object sender, EventArgs e)
    {

        if ((string)Session["Baimasoft_User_LogonID"] == null)
        {
            Response.Redirect("../ReLogon.aspx");
        }
        try
        {
            sCodeCol = Request.QueryString["CodeCol"].ToString();
        }
        catch
        {
        }

        try
        {
            sThisCode = Request.QueryString["ThisCode"].ToString();
            HThisCode.Value = sThisCode;
        }
        catch
        {
        }

        if (sCodeCol == "" || sCodeCol == null)
        {
            sCodeCol = "0";
        }
        UserID = (string)Session["Baimasoft_User_LogonID"];
        UserRole = (string)Session["Baimasoft_Role_Code"];


        if (!IsPostBack)
        {
            if (ViewState["VLogonID"] == null || ViewState["VLogonID"].ToString().CompareTo("") == 0)
            {
                ViewState["VLogonID"] = "";
            }
            if (ViewState["VRoleName"] == null || ViewState["VRoleName"].ToString().CompareTo("") == 0)
            {
                ViewState["VRoleName"] = "0";
            }

            if (ViewState["OpType"] == null)
            {
                ViewState["OpType"] = "1";
            }

            if (ViewState["SortExpression"] == null || ViewState["SortExpression"].ToString().CompareTo("") == 0)
            {
                ViewState["SortExpression"] = "LogonID";
            }

            if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
            {
                ViewState["SortDirection"] = " ";
            }
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            RoleBind(UserRole);
            Bind(UserID, sSqlSort);
        }
    }

    //默认数据绑定
    public void RoleBind(string sUserRole)
    {
        RoleName.DataSource = MyRM.GetMyCtrlRole(sUserRole, "1", "");
        RoleName.DataBind();
        RoleName.Items.Insert(0, "全部角色");
        RoleName.Items[0].Value = "0";
    }

    private void Bind(string sUserID, string SqlSort)
    {
        if (ViewState["OpType"].ToString() == "0")
        {
            MyDV = MYUM.GetMyUserInfo(sUserID, sCodeCol, "1", SqlSort);
        }
        else if (ViewState["OpType"].ToString() == "1")  //查询状态
        {
            MyDV = MYUM.SearchUserInfo(ViewState["VLogonID"].ToString().Trim(), ViewState["VRoleName"].ToString().Trim(), sCodeCol, sUserID, SqlSort, "");
        }
        else if (ViewState["OpType"].ToString() == "2") // 显示删除
        {
            MyDV = MYUM.SearchUserInfo(ViewState["VLogonID"].ToString().Trim(), ViewState["VRoleName"].ToString().Trim(), sCodeCol, sUserID, SqlSort, "0','1");
        }
        GridView1.DataSource = MyDV;
        GridView1.DataBind();
        AllPageCount = GridView1.PageCount;
        NowPageCount = GridView1.PageIndex + 1;
        AllCount = MyDV.Count;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserID, sSqlSort);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            if (e.Row.RowIndex % 2 != 0)
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text + "|" + e.Row.Cells[1].Text + "|" + e.Row.Cells[2].Text + "|" + e.Row.Cells[3].Text + "|" + e.Row.Cells[4].Text + "|" + e.Row.Cells[5].Text + "|" + e.Row.Cells[6].Text + "|" + e.Row.Cells[7].Text + "|" + e.Row.Cells[8].Text + "|" + e.Row.Cells[9].Text + "|" + e.Row.Cells[10].Text + "|" + e.Row.Cells[11].Text + "|" + e.Row.Cells[12].Text + "|" + e.Row.Cells[13].Text + "|" + e.Row.Cells[14].Text + "|" + e.Row.Cells[15].Text + "'),tog(this,'AliceBlue')");
            }
            else
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text + "|" + e.Row.Cells[1].Text + "|" + e.Row.Cells[2].Text + "|" + e.Row.Cells[3].Text + "|" + e.Row.Cells[4].Text + "|" + e.Row.Cells[5].Text + "|" + e.Row.Cells[6].Text + "|" + e.Row.Cells[7].Text + "|" + e.Row.Cells[8].Text + "|" + e.Row.Cells[9].Text + "|" + e.Row.Cells[10].Text + "|" + e.Row.Cells[11].Text + "|" + e.Row.Cells[12].Text + "|" + e.Row.Cells[13].Text + "|" + e.Row.Cells[14].Text + "|" + e.Row.Cells[15].Text + "'),tog(this,'#ffffff')");
            }
            if (e.Row.Cells[9].Text == "0")
                e.Row.ForeColor = Color.Red;
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"].ToString() == "desc")
        {
            ViewState["SortDirection"] = " ";
        }
        else
        {
            ViewState["SortDirection"] = "desc";
        }

        ViewState["SortExpression"] = e.SortExpression;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserID, sSqlSort);
    }
    protected void SearchUserInfo_ServerClick(object sender, EventArgs e)
    {
        ViewState["OpType"] = "1";
        ViewState["VLogonID"] = LogonID.Text.ToString().Trim();
        ViewState["VRoleName"] = RoleName.SelectedValue.ToString().Trim();
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserID, sSqlSort);
    }

    public static string Md5(string str)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
    }

    protected void BtnExport_Click(object sender, EventArgs e)
    {
        string sFileDT = string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now);
        string sFileName = "UserInfo(" + sFileDT + ").xls";

        ViewState["VLogonID"] = LogonID.Text.ToString().Trim();
        ViewState["VRoleName"] = RoleName.SelectedValue.ToString().Trim();
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];

        MyDV = MYUM.SearchUserInfo(ViewState["VLogonID"].ToString().Trim(), ViewState["VRoleName"].ToString().Trim(), sCodeCol, UserID, sSqlSort, "");
        Response.Buffer = true;
        Response.ClearContent();
        //指定http名称和值 
        Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(sFileName, System.Text.Encoding.UTF8) + ".xls");
        //指定文件类型
        Response.ContentType = "application/excel";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        StringWriter sw = new StringWriter();
        GridView2.DataSource = MyDV;
        GridView2.DataBind();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView2.RenderControl(htw);
        //GridView1.RenderControl(htw);
        string style = @"<style> .text { mso-number-format:\@; } </script> "; //单元格式为文本格式



        //sw写入到http输出流



        Response.Output.Write(style);
        Response.Output.Write(sw.ToString());

        Response.Flush();
        Response.End();
        GridView2.Visible = false;
        //DrawExcelFromDataGrid(MYUM.SearchUserInfo(ViewState["VLogonID"].ToString().Trim(), ViewState["VRoleName"].ToString().Trim(),sCodeCol, UserID, sSqlSort), sFileName);
    }

    /// 从GridView中导出到Excel中去
    private void DrawExcelFromDataGrid(DataView dv, string FileName)
    {
        HttpResponse resp;
        resp = Page.Response;
        resp.ContentEncoding = System.Text.Encoding.Default;
        resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
        string colHeaders = "", ls_item = "";

        string sHeading = "用户帐户|别名|真实姓名|角色名称|用户组|创建者";
        string sColumns = "1|2|13|15|12|10";
        string[] AHeading = sHeading.Split('|');
        int iHeading = AHeading.Length;
        for (int i = 0; i < iHeading - 1; i++)
        {
            colHeaders += AHeading[i] + Convert.ToChar(9);
        }
        colHeaders += AHeading[iHeading - 1] + Convert.ToChar(13);
        resp.Write(colHeaders);


        GridView MyGV = new GridView();
        MyGV.DataSource = dv;
        MyGV.DataBind();

        string[] AColumns = sColumns.Split('|');
        int iColumns = AColumns.Length;

        foreach (GridViewRow row in MyGV.Rows)
        {
            for (int j = 0; j < iColumns - 1; j++)
            {
                int k = Convert.ToInt32(AColumns[j]);
                ls_item = ls_item + backstring(row.Cells[k].Text.ToString().Trim()) + Convert.ToChar(9);
            }
            int p = Convert.ToInt32(AColumns[iColumns - 1]);
            ls_item = ls_item + backstring(row.Cells[p].Text.ToString().Trim()) + Convert.ToChar(13);
            resp.Write(ls_item);
            ls_item = "";
        }
        resp.End();
    }

    protected void BtnDel_Click(object sender, EventArgs e)
    {
        string sUserID = HUserCode.Value.ToString().Trim();
        int iback = MyUM.IFDelUserInfo(sUserID, "0");
        if (iback == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功!');</script>");
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(UserID, sSqlSort);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败!');</script>");
        }
    }
    protected void BtnDispDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "2";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserID, sSqlSort);
    }
    protected void BtnHideDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "0";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserID, sSqlSort);
    }
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
    }
    protected void BtnResume_Click(object sender, EventArgs e)
    {
        string sResumeCode = HUserCode.Value.ToString().Trim();
        int iback = MyUM.IFDelUserInfo(sResumeCode, "1");
        if (iback == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复成功!');</script>");
            ViewState["OpType"] = "2";
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(UserID, sSqlSort);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复失败!');</script>");
        }
    }

    public string ListState(string str1)
    {
        if (str1 == "1")
            return "是";
        else
            return "<font color=red>否</font>";
    }
    protected void BtnOriPsd_Click(object sender, EventArgs e)
    {
        string sPassword = MyFile.Md5("111111"); //用户初始化密码
        string sUserID = HUserCode.Value.ToString().Trim();//用户ID
        int iback = MyUM.OriUserPsd(sUserID, sPassword);
        if (iback > 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('还原成功!');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('还原失败!');</script>");
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }


    protected string backstring(string stmp)
    {
        if (stmp == "&nbsp;")
        {
            stmp = "";
        }
        return stmp;
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.Add("class", "text");
            e.Row.Cells[2].Attributes.Add("class", "text");
            e.Row.Cells[3].Attributes.Add("class", "text");
        }
    }
    //---------------------------2010.2.23 李伟 增加永久删除功能  START---------------------------------//
    protected void BtnYJDel_Click(object sender, EventArgs e)
    {
        string sUserID = HUserCode.Value.ToString().Trim();
        HsTec.Mobile.Green.BLL.UserManager um = new HsTec.Mobile.Green.BLL.UserManager();
        int iback = um.delUserInfo(sUserID);
        if (iback == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('永久删除成功!');</script>");
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(UserID, sSqlSort);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('永久删除失败!');</script>");
        }
    }
    //---------------------------2010.2.23 李伟 增加永久删除功能  END---------------------------------//
}
